//
//  OtherPersonViewController.m
//  Comma
//
//  Created by 杜远洋 on 15-1-30.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "OtherPersonViewController.h"
#import "OtherPersonView.h"
#import "EditDetailViewController.h"
#import "MyDynamicViewController.h"
#import "AFNetWorkingYU.h"
#import "GlobalMethod.h"
#import "URLMacro.h"
#import "ConnectModel.h"
#import "LoadIndicator.h"
#import "Reachability.h"
@interface OtherPersonViewController ()

@end

@implementation OtherPersonViewController

- (void)loadView
{
    self.personalCenter = [[OtherPersonView alloc] initWithLeftButton:@"返回" RightButton:nil Title:@"个人中心" FooterView:FooterViewStateNone];
    self.view = self.personalCenter;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createMyCenter];
    [self getDataFromServer];
    [self.view setBackgroundColor:[UIColor whiteColor]];
}
- (void)createMyCenter
{
   
    self.leftButton = self.personalCenter.leftButton;
    
}

#pragma mark - 从服务器获取数据(给所有控件初始值)
- (void)getDataFromServer
{
    NSString *key = [GlobalMethod getApproveKey];
    NSString *device = [GlobalMethod GetIOSUUID];
    
    NSString *str =[NSString stringWithFormat:@"approveKey=%@&deviceParam=%@&userId=%@", device, key, self.puberId];
    NSString *getUrl = [NSString stringWithFormat:@"%@?%@", @"http://www.isu.so/user/mobi_getdetail_by_userid", str];
    
    Reachability * reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];
    if ([reach currentReachabilityStatus] != NotReachable) {

    [AFNetWorkingYU netWorkingHanderGetGataWithUrl:getUrl resultBlock:^(id result) {
        if ([result isKindOfClass:[NSDictionary class]]) {
        
        NSDictionary *dic = (NSDictionary *)result;
        NSString *str = [result objectForKey:@"msg"];
        [LoadIndicator stopAnimationInView:self.view];
        str = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"%@", str);
        if ([[dic objectForKey:@"msg"] isEqualToString:@"success"])
        {
            NSDictionary *my = [dic objectForKey:@"obj"];
            for (NSString *key in my.allKeys) {
                if ([[my objectForKey:key] isKindOfClass:[NSNull class]]) {
                    [my setValue:@"" forKey:key];
                }
            }
            
            self.personalCenter.InforDic = [NSDictionary dictionaryWithDictionary:my];
            

            
            [self.leftButton addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            //我的动态点击事件（背景条）
            
        }
            
        }
        else
        {
            UIView *fondView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
            [fondView setBackgroundColor:[UIColor whiteColor]];
            [self.view addSubview:fondView];
            [LoadIndicator networkStateNotReachableInView:fondView];
        }
    }];
    }
    else
    {
        UIView *fondView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
        [fondView setBackgroundColor:[UIColor whiteColor]];
        [self.view addSubview:fondView];
        [LoadIndicator networkStateNotReachableInView:fondView];
    }
    
}


//返回
- (void)leftButtonAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}




- (NSString *)getMyAgeAndSex
{
    NSString *age = [self.myDic objectForKey:@"birth"];
    age = [self getMyAge:age];
    NSString *gender = [self.myDic objectForKey:@"gender"];
    
    if ([age isEqualToString:@""] && ![gender isEqualToString:@""]) {
        NSString *ageAndGender = [NSString stringWithFormat:@"    %@", gender];
        return ageAndGender;
    }
    else if ( [gender isEqualToString:@""] && ![age isEqualToString:@""]) {
        NSString *ageAndGender = [NSString stringWithFormat:@"%@   ", age];
        return ageAndGender;
    }
    else if ( [age isEqualToString:@""] && [gender isEqualToString:@""])
    {
        
        return nil;
    }
    else
    {
        NSString *ageAndGender = [NSString stringWithFormat:@"%@ %@", age, gender];
        return ageAndGender;
    }
}

#pragma mark - 根据生日 获取年龄
- (NSString *)getMyAge:(NSString *)birthday
{
    
    NSString *timeStr = birthday;
    //得到数组的年月日
    NSArray *myBirth = [birthday componentsSeparatedByString:@"-"];
    NSLog(@"%@", myBirth);
    
    
    
    
    NSDateFormatter *date=[[NSDateFormatter alloc] init];
    [date setDateFormat:@"YY-MM-dd"];
    NSDate *d=[date dateFromString:timeStr];
    
    NSTimeInterval late=[d timeIntervalSince1970]*1;
    
    
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval now=[dat timeIntervalSince1970]*1;
    NSString *timeString=@"";
    
    NSTimeInterval cha=now-late;
    
    if (cha/86400>1)
    {
#pragma mark - fuck!! 次数不需要计算了   因为是从自己生日那天算起的 除以365的时候余数是从生日开始的
        /*
         NSInteger day =  cha / 86400;
         NSInteger year = day / 365;
         NSInteger myDays = [self calculateYear:[[myBirth firstObject] integerValue] MonthDays:[[myBirth objectAtIndex:1] integerValue] days:[[myBirth lastObject] integerValue]];
         if ((day % 365) > myDays) {
         year ++;
         }
         */
        
        timeString = [NSString stringWithFormat:@"%f", cha/86400/365 + 1];
        timeString = [timeString substringToIndex:timeString.length-7];
        timeString=[NSString stringWithFormat:@"%@", timeString];
    }
    
    return timeString;
}


#pragma mark - 每次页面出现的时候都会先刷新数据
- (void)viewWillAppear:(BOOL)animated
{
       //如果视图已经存在  那么就不需要在此进行加载了
    if (!self.personalCenter) {
        [LoadIndicator addIndicatorInView:self.view];
        
    }
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
